import 'package:flutter/material.dart';

/// 这是一个可以指定SafeArea区域背景色的AppBar
/// PreferredSizeWidget提供指定高度的方法
/// 如果没有约束其高度，则会使用PreferredSizeWidget指定的高度

class CustomAppbar extends StatelessWidget implements PreferredSizeWidget {
  final double contentHeight; //从外部指定高度
  Color navigationBarBackgroundColor; //设置导航栏背景的颜色
  Widget leadingWidget;
  Widget trailingWidget;
  String title;
  double titleSize;

  CustomAppbar({
    super.key,
    required this.leadingWidget,
    required this.title,
    this.titleSize = 20,
    this.contentHeight = 50,
    this.navigationBarBackgroundColor = Colors.white,
    required this.trailingWidget,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      color: navigationBarBackgroundColor,
      child: SafeArea(
        top: true,
        child: Container(
            decoration: const UnderlineTabIndicator(
              borderSide: BorderSide(width: 1.0, color: Color(0xFFeeeeee)),
            ),
            height: contentHeight,
            child: Stack(
              alignment: Alignment.center,
              children: <Widget>[
                Positioned(
                  left: 0,
                  child: Container(
                    padding: const EdgeInsets.only(left: 5),
                    child: leadingWidget,
                  ),
                ),
                Text(title,
                    style: TextStyle(
                      color: const Color(0xFF333333),
                      fontSize: titleSize,
                    )),
                Positioned(
                  right: 0,
                  child: Container(
                    padding: const EdgeInsets.only(right: 5),
                    child: trailingWidget,
                  ),
                ),
              ],
            )),
      ),
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(contentHeight);
}
